package shiyanbaogao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class EmployeeDAO {

    private static final String URL = "jdbc:mysql://localhost:3306/usersdb";
    private static final String username = "root";
    private static final String PASSWORD = "123456";

    public void saveEmployee(Employee employee) {
        String sql = "INSERT INTO employees (id, name, password, age) VALUES (?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(URL, username, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, employee.getId());
            pstmt.setString(2, employee.getName());
            pstmt.setString(3, employee.getPassword());
            pstmt.setInt(4, employee.getAge());

            int rowsAffected = pstmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("Employee saved successfully.");
            } else {
                System.out.println("Failed to save employee.");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EmployeeDAO dao = new EmployeeDAO();
        Employee employee = new Employee(1, "张三", "password123", 30);
        dao.saveEmployee(employee);
    }
}


